package se.id.vrapp;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Taken from
 * http://stackoverflow.com/questions/13992535/android-imageview-scale
 * -smaller-image-to-width-with-flexible-height-without-crop by the author Mark
 * Martinsson. But modified :D
 * 
 */
public class DynamicImageView extends ImageView {

	public DynamicImageView(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(final int widthMeasureSpec,
			final int heightMeasureSpec) {
		final Drawable d = this.getDrawable();

		if (d != null) {
			final int height = MeasureSpec.getSize(heightMeasureSpec);
			final int width = (int) Math.ceil(height
					* (float) d.getIntrinsicWidth() / d.getIntrinsicHeight());
			this.setMeasuredDimension(width, height);
		} else {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
	}
}